﻿using System.IO.Compression;
using System.IO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using System.Reflection;

namespace Marbles.Core.Web
{
    public class Application : System.Web.HttpApplication
    {
        protected void Application_Start(object sender, EventArgs e)
        {
            this.PreRequestHandlerExecute += new EventHandler(Application_PreRequestHandler);
        }

        public override void Init()
        {
            base.Init();
            this.PreRequestHandlerExecute += new EventHandler(Application_PreRequestHandler);
        }

        protected void Session_Start(object sender, EventArgs e)
        {

        }

        protected void Application_BeginRequest(object sender, EventArgs e)
        {

        }

        protected void Application_AuthenticateRequest(object sender, EventArgs e)
        {

        }

        protected void Application_Error(object sender, EventArgs e)
        {

        }

        protected void Session_End(object sender, EventArgs e)
        {

        }

        protected void Application_End(object sender, EventArgs e)
        {

        }

        protected void Application_PreRequestHandler(object sender, EventArgs e)
        {
            bool EnablePageCompression = false;
            if (System.Web.Configuration.WebConfigurationManager.AppSettings["Marbles.PageCompressionEnabled"] != null)
            {
                if (System.Web.Configuration.WebConfigurationManager.AppSettings["Marbles.PageCompressionEnabled"].ToLower() == "true") { EnablePageCompression = true; }
            }

            if (!EnablePageCompression) { return; }

            HttpApplication app = (HttpApplication)sender;
            string acceptEncoding = app.Request.Headers["Accept-Encoding"];
            Stream prevUncompressedStream = app.Response.Filter;

            if ((app.Context.CurrentHandler.GetType() != typeof(System.Web.UI.Page)) || (app.Request["HTTP_X_MICROSOFT_AJAX"] != null)) { return; }

            if (app.Request["X-MicrosoftAjax"] != null)
            {
                if (app.Request["X-MicrosoftAjax"] == "delta-true") { return; }
            }

            if ((acceptEncoding == null) || (acceptEncoding.Length == 0)) { return; }

            acceptEncoding = acceptEncoding.ToLower();

            if ((acceptEncoding.Contains("deflate")) || (acceptEncoding == "*"))
            {
                app.Response.Filter = new DeflateStream(prevUncompressedStream, CompressionMode.Compress);
                app.Response.AppendHeader("Content-Encoding", "deflate");
            }
            else
            {
                app.Response.Filter = new GZipStream(prevUncompressedStream, CompressionMode.Compress);
                app.Response.AppendHeader("Content-Encoding", "gzip");
            }

        }

    }
}
